/* add user code begin Header */
/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "at32f413_wk_config.h"
#include "wk_adc.h"
#include "wk_debug.h"
#include "wk_tmr.h"
#include "wk_usart.h"
#include "wk_dma.h"
#include "wk_system.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "FOC.h"
#include "calculation_speed.h"
/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */
uint8_t usart2_rx_buffer[DMA1_CHANNEL1_BUFFER_SIZE];



/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

/* add user code end 0 */

/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
  /* add user code begin 1 */

  /* add user code end 1 */

  /* system clock config. */
  wk_system_clock_config();

  /* config periph clock. */
  wk_periph_clock_config();

  /* init debug function. */
  wk_debug_config();

  /* nvic config. */
  wk_nvic_config();

  /* timebase config. */
  wk_timebase_init();

  /* init dma1 channel1 */
  wk_dma1_channel1_init();
  /* config dma channel transfer parameter */
  /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
  wk_dma_channel_config(DMA1_CHANNEL1, 
                        (uint32_t)&USART3->dt, 
                        DMA1_CHANNEL1_MEMORY_BASE_ADDR, 
                        DMA1_CHANNEL1_BUFFER_SIZE);
  dma_channel_enable(DMA1_CHANNEL1, TRUE);

  /* init usart3 function. */
  wk_usart3_init();

  /* init adc1 function. */
  wk_adc1_init();

  /* init tmr1 function. */
  wk_tmr1_init();

  /* init tmr2 function. */
  wk_tmr2_init();

  /* init tmr3 function. */
  wk_tmr3_init();

  /* init tmr4 function. */
  wk_tmr4_init();

  /* add user code begin 2 */
    //初始化FOC
    board_config();
    
  //dma_interrupt_enable(DMA1_CHANNEL1, DMA_FDT_INT, TRUE);


  /* add user code end 2 */

  while(1)
  {
    /* add user code begin 3 */

    //usart_data_transmit(USART3, 0x11);
    
    //printf("da\n");
//    foc_current_loop();
    //wk_delay_ms(1);
    //16k
    if(is_upcount)
    {
      is_upcount=0;
      

      // if(State==START)
      // {
      //   Start_Up();
      // }
      // else if(State==RUN)
      // {
      //   foc_current_loop();
      // }
      // else if(State==STOP)
      // {
      //   Stop();
      // }
      state_switch();


      static uint8_t task_4k=0;
      task_4k++;
      if(task_4k==4)
      {
        task_4k=0;
//        if(State==RUN)
//        {
//          foc_speed_loop();
//        }
        //foc_speed_loop();
        
        
      }


     
      
      
      
    //foc_current_loop();
    }
    
    
    if(task_1ms)
    {
      task_1ms=0;

      if(State==RUN)
      {
        foc_speed_loop();
      }
      
      speed_computer(gtim_get_encode(),1);
      speed_computer_encode(&myMotorState, gtim_get_encode(), 1, 1.0f,ENCODE_COUNT , 1);
//      //处理串口回传的数据
//      if(usart2_rx_dma_status)
//      {
//        usart2_rx_dma_status=0;
//        //printf("usart2_rx_buffer:%s\n",usart2_rx_buffer);
//       
//        process_received_data(usart2_rx_buffer,10);
//        dma_channel_enable(DMA1_CHANNEL1, TRUE);
//
//      }
      
      
      
      
    }
    
    
    if(task_10ms)
    {
      task_10ms=0;
      
      //printf("%.0f,%d\n",speed_motor,speed_set);
      //speed_computer(gtim_get_encode(),1);
      if(usart2_rx_buffer[0]!=0)
      {
        //dma_channel_enable(DMA1_CHANNEL1, FALSE);
        process_received_data(usart2_rx_buffer,DMA1_CHANNEL1_BUFFER_SIZE);
        //dma_channel_enable(DMA1_CHANNEL1, TRUE);
        wk_dma1_channel1_init();
        wk_dma_channel_config(DMA1_CHANNEL1, 
                        (uint32_t)&USART3->dt, 
                        DMA1_CHANNEL1_MEMORY_BASE_ADDR, 
                        DMA1_CHANNEL1_BUFFER_SIZE);
        dma_channel_enable(DMA1_CHANNEL1, TRUE);
      }
    }
    
    
    
    
    
    /* add user code end 3 */
  }
}

  /* add user code begin 4 */

  /* add user code end 4 */
